import re

file_path = "装配式厂房.ifc"

input_ifc_file = "装配式厂房.ifc"
output_ifc_file = 'path_to_output.ifc'


# 把 ifc 中文编码转义，自己查看的
def decode_utf16_text(encoded_text):
    def decode_match(match):
        hex_str = match.group(1)
        # 确保字节数是偶数
        if len(hex_str) % 4 != 0:
            hex_str = hex_str[:-(len(hex_str) % 4)]
        utf16_bytes = bytes.fromhex(hex_str)
        try:
            return utf16_bytes.decode('utf-16-be')  # 使用big-endian解码
        except UnicodeDecodeError:
            return match.group(0)  # 返回原始匹配的文本以防解码失败

    decoded_text = re.sub(r'\\X2\\([0-9A-F]+)\\X0\\', decode_match, encoded_text)
    return decoded_text


def replace_utf16_with_chinese(input_file_path, output_file_path):
    with open(input_file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # 解码UTF-16编码的部分
    decoded_content = decode_utf16_text(content)

    with open(output_file_path, 'w', encoding='utf-8') as file:
        file.write(decoded_content)


# 替换UTF-16编码并保存文件
replace_utf16_with_chinese(input_ifc_file, output_ifc_file)
